$(document).ready(function() {
    if (!createjs.Sound.initializeDefaultPlugins()) {
        return;
    }

    createjs.Sound.alternateExtensions = ["mp3"];
    var url = $('#listen').attr('data-url');
    createjs.Sound.registerSound(url, "sound");
    $('#listen').click(function() {
        createjs.Sound.play("sound");
        return false;
    });
    $('#btn-check').click(function() {
        var baseUrl = $('#base-url').attr('href');
        var questions = $('.question-number').toArray();
        var qts = [];
        questions.forEach(function(question) {
            qts.push($(question).attr('data-id'));
        });
        var document = $('#document-content').attr('data-id');
        $.ajax({
            url: baseUrl + '/practice/getAnswer',
            type: 'POST',
            data: {questions: qts.join(','), document: document},
            success: function(data) {
                var result = JSON.parse(data);
                if(result.doc){
                    $('#document-content').html(result.doc.content);
                    for(var i in result.answer){
                        $('#answer'+result.answer[i].toLowerCase()+i).parent().toggleClass('correct');
                    }
                }
            }
        });
        return false;
    });
});